package com.xxc.domain.strategy.model.entity;

import com.xxc.types.common.Constants;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: xxc
 * @Description: 策略规则实体
 * @Date: 2025/1/21 22:33
 * @Version: 1.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class StrategyRuleEntity {

    /** 抽奖策略ID */
    private Long strategyId;
    /** 抽奖奖品ID【规则类型为策略，则不需要奖品ID】 */
    private Integer awardId;
    /** 抽象规则类型；1-策略规则、2-奖品规则 */
    private Integer ruleType;
    /** 抽奖规则类型【rule_random - 随机值计算、rule_lock - 抽奖几次后解锁、rule_luck_award - 幸运奖(兜底奖品)】 */
    private String ruleModel;
    /** 抽奖规则比值 */
    private String ruleValue;
    /** 抽奖规则描述 */
    private String ruleDesc;

    /**
     * 获取权重值
     * 数据案例；4000:102,103,104,105 5000:102,103,104,105,106,107 6000:102,103,104,105,106,107,108,109
     */
    public Map<String, List<Integer>> getRuleWeightValues(){
        if(!"rule_weight".equals(ruleModel)) return null;

        String[] ruleValueGroups = ruleValue.split(Constants.SPACE);
        Map<String, List<Integer>> resultMap = new HashMap<>();

        for (String ruleValueGroup : ruleValueGroups) {
            // 检查输入是否为空
            if(ruleValueGroup==null||ruleValueGroup.isEmpty()){
                return resultMap;
            }

            // 分割字符串获取键和值
            String[] creditAndWeights = ruleValueGroup.split(Constants.COLON);
            if(creditAndWeights.length!=2){
                throw new IllegalArgumentException("rule_weight rule_rule invalid input format" + ruleValueGroup);
            }
            // 解析值
//            String credit = creditAndWeights[0];
            String[] valueStrings = creditAndWeights[1].split(Constants.SPLIT);
            List<Integer> values = Arrays.stream(valueStrings).map(Integer::parseInt).collect(Collectors.toList());

            // 将键和值放进Map中, key[4000:102,103] value[{102,103}]
            resultMap.put(ruleValueGroup, values);
        }

        return resultMap;
    }


/*    public String[] getRuleBlacklist() {
        if(!"rule_blacklist".equals(ruleModel)) return null;

        String[] splitRuleValue = ruleValue.split(Constants.COLON);
        splitRuleValue
        return null;
    }*/
}
